package com.yao.classloader;

import java.io.File;
import java.lang.reflect.Method;

public class HotswapTest {

	public static void main(String[] args) throws Exception {
		loadHelloWorld();
		System.gc();
		Thread.sleep(1000);// 等待资源被回收
		File fileV2 = new File("HelloWorld.class");
		File fileV1 = new File("bin\\com\\yao\\classloader\\HelloWorld.class");
		fileV1.delete();
		fileV2.renameTo(fileV1);
		System.out.println("Update success!");
		loadHelloWorld();
	}

	public static void loadHelloWorld() throws Exception {
		MyClassLoader myLoader = new MyClassLoader(); // 自定义类加载器
		Class<?> class1 = myLoader.findClass("com.yao.classloader.HelloWorld");// 类实例
		Object obj1 = class1.newInstance(); // 生成新的对象
		Method method = class1.getMethod("say");
		method.invoke(obj1); // 执行方法say
		System.out.println(obj1.getClass()); // 对象
		System.out.println(obj1.getClass().getClassLoader()); // 对象的类加载器
	}
}
